package com.itmate.wei.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.itmate.wei.db.DBUtil;
import com.itmate.wei.entity.MessageBoard;
import com.itmate.wei.entity.Statues;


public class MessageBoardDao extends BaseDao{
	public List<MessageBoard> SelectAllMB(){
		List<MessageBoard> statuelist=new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from message_board ";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				MessageBoard MB = new MessageBoard();
				MB.setId(resultSet.getInt("id"));
				MB.setContent(resultSet.getString("content"));
				MB.setTime(resultSet.getString("time"));
				MB.setMes_p(resultSet.getString("mes_p"));
				statuelist.add(MB);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		
		return statuelist;
	}
	
	public int addMessageBoard(MessageBoard sta){
			int count = 0;
		 try {
			connection = DBUtil.getConnection();
			String sql = "insert into message_board(content,time,mes_p) values(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, sta.getContent());
			preparedStatement.setString(2, sta.getTime());
			preparedStatement.setString(3, sta.getMes_p());
			count = preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		return count;
	}
	
	public int DeleteMesByID(int id){
		int i=-1;
		try {
			connection=DBUtil.getConnection();
			preparedStatement=connection.prepareStatement(
					"delete from message_board where id=?");
			preparedStatement.setInt(1, id);
			i=preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
}
